package com.tiancheng.trade.commom.core.exception;


import com.tiancheng.trade.commom.core.exception.error.IErrorInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * @author jiangjun
 * @description 断言处理类，用于抛出各种API异常
 * @date 2024/10/14 16:26
 */
public class Asserts {

    public static void fail(String message) {
        throw new BusinessException(message);
    }

    public static void fail(IErrorInfo errorCode) {
        throw new BusinessException(errorCode);
    }

    public static void isNotNull(Object object, int errorCode, String errorMessage) {
        if (object == null) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isNotNull(Object object, String errorMessage) {
        isNotNull(object, 500, errorMessage);
    }

    public static void isNull(Object object, int errorCode, String errorMessage) {
        if (object != null) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isNull(Object object, String errorMessage) {
        isNull(object, 500, errorMessage);
    }

    public static void isNotBlank(CharSequence object, int errorCode, String errorMessage) {
        if (StringUtils.isBlank(object)) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isNotBlank(CharSequence object, String errorMessage) {
        isNotBlank(object, 500, errorMessage);
    }

    public static void isBlank(CharSequence object, int errorCode, String errorMessage) {
        if (!StringUtils.isBlank(object)) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isBlank(CharSequence object, String errorMessage) {
        isBlank(object, 500, errorMessage);
    }

    public static void isNotEmpty(CharSequence object, int errorCode, String errorMessage) {
        if (StringUtils.isEmpty(object)) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isNotEmpty(CharSequence object, String errorMessage) {
        isNotEmpty((CharSequence)object, 500, errorMessage);
    }

    public static void isEmpty(CharSequence object, int errorCode, String errorMessage) {
        if (!StringUtils.isEmpty(object)) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isEmpty(CharSequence object, String errorMessage) {
        isEmpty((CharSequence)object, 500, errorMessage);
    }

    public static void isNotEmpty(Collection<?> object, int errorCode, String errorMessage) {
        if (object == null || object.isEmpty()) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isNotEmpty(Collection<?> object, String errorMessage) {
        isNotEmpty((Collection)object, 500, errorMessage);
    }

    public static void isEmpty(Collection<?> object, int errorCode, String errorMessage) {
        if (object != null && !object.isEmpty()) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isEmpty(Collection<?> object, String errorMessage) {
        isEmpty((Collection)object, 500, errorMessage);
    }

    public static void isNotEmpty(Map<?, ?> object, int errorCode, String errorMessage) {
        if (object == null || object.isEmpty()) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isNotEmpty(Map<?, ?> object, String errorMessage) {
        isNotEmpty((Map)object, 500, errorMessage);
    }

    public static void isEmpty(Map<?, ?> object, int errorCode, String errorMessage) {
        if (object != null && !object.isEmpty()) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isEmpty(Map<?, ?> object, String errorMessage) {
        isEmpty((Map)object, 500, errorMessage);
    }

    public static void isTrue(boolean condition, String errorMessage) {
        isTrue(condition,500,errorMessage);
    }

    public static void isTrue(boolean object, int errorCode, String errorMessage) {
        if (!object) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isFalse(boolean object, int errorCode, String errorMessage) {
        if (object) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void isFalse(boolean object, String errorMessage) {
        isFalse(object, 500, errorMessage);
    }
}
